<template>
  <div class="container">
    <h1>登录</h1>
    <el-form :model="loginForm" ref="loginForm">
      <el-form-item
        label="手机号码"
        prop="phone"
        :rules="[
          { required: true, message: '请输入手机号码', trigger: 'blur' },
        ]"
      >
        <el-input v-model="loginForm.phone" autocomplete="off" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login()">声纹登录</el-button>
        <el-link
          type="primary"
          :underline="false"
          style="margin-left: 10px"
          @click="regist()">声纹注册</el-link>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import qs from 'qs'

export default {
  name: 'login',
  data () {
    return {
      loginForm: {
        phone: ''
      }
    }
  },
  methods: {
    login () {
      this.axios({
        method: 'POST',
        url: '/kst/exist',
        headers: { 'content-type': 'application/x-www-form-urlencoded' },
        data: qs.stringify({
          type: 'rd',
          node: 'node1',
          spkid: this.loginForm.phone
        })
      }).then((response) => {
        if (response.data.code === 0) {
          if (response.data.exist) {
            this.$refs.loginForm.validate((valid) => {
              if (valid) {
                window.sessionStorage.setItem('spkid', this.loginForm.phone)
                this.$router.push({
                  path: '/login/voice'
                })
              } else {
                return false
              }
            })
          } else {
            this.$notify({
              title: '提示',
              message: '账号未注册',
              type: 'warning'
            })
          }
        } else {
          this.$notify.error({
            title: '失败',
            message: response.data.msg
          })
        }
      })
    },
    regist () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          window.sessionStorage.setItem('spkid', this.loginForm.phone)
          this.$router.push({
            path: '/login/voice-regist'
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
.container {
  width: 400px;
  padding: 20px 50px;
}
</style>
